﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Data;
using produccion.reportes;
using produccion.vistamodelo;

namespace produccion.forms
{
    /// <summary>
    /// Lógica de interacción para FRManaConStn.xaml
    /// </summary>
    public partial class FRManaConStn : Window
    {
        public FRManaConStn()
        {
            InitializeComponent();
        }
        private void butPro_Click(object sender, RoutedEventArgs e)
        {
            anaConStn.Instance.procesar();
        }
        private void butImp_Click(object sender, RoutedEventArgs e)
        {
            anaConStn.Instance.imprimir();
        }
        private void butSal_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        private void Window_Closed(object sender, EventArgs e)
        {
            anaConStn.Instance = null;
        }

        private void datInf_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            anaConStn.Instance.consDetalle();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Keyboard.Focus(datDes);
        }

        private void datInf_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            var elemento = e.OriginalSource as UIElement;
            if (e.Key == Key.Enter && elemento != null)
            {
                e.Handled = true;
                elemento.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
            }
        }

        //
        // SINGLE CLICK EDITING
        //
        private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DataGridCell cell = sender as DataGridCell;
            if (cell != null && !cell.IsEditing && !cell.IsReadOnly)
            {
                if (!cell.IsFocused)
                {
                    cell.Focus();
                }
                DataGrid dataGrid = FindVisualParent<DataGrid>(cell);
                if (dataGrid != null)
                {
                    if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
                    {
                        if (!cell.IsSelected)
                            cell.IsSelected = true;
                    }
                    else
                    {
                        DataGridRow row = FindVisualParent<DataGridRow>(cell);
                        if (row != null && !row.IsSelected)
                        {
                            row.IsSelected = true;
                        }
                    }
                }
            }
        }

        static T FindVisualParent<T>(UIElement element) where T : UIElement
        {
            UIElement parent = element;
            while (parent != null)
            {
                T correctlyTyped = parent as T;
                if (correctlyTyped != null)
                {
                    return correctlyTyped;
                }

                parent = VisualTreeHelper.GetParent(parent) as UIElement;
            }
            return null;
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            var elemento = e.OriginalSource as UIElement;
            if (e.Key == Key.F3 && elemento != null)
            {
                if (e.Key == Key.F3)
                {
                    if (MDOgeneral.logBusquedadAbierta == true) { MDOgeneral.logBusquedadAbierta = false; }
                }
            }

            if (e.Key == Key.F4 && elemento != null)
            {
                if (e.Key == Key.F4)
                {
                    if (MDOgeneral.logBusquedadAbierta1 == true) { MDOgeneral.logBusquedadAbierta1 = false; }
                }
            }

            if (e.Key == Key.F5 && elemento != null)
            {
                if (e.Key == Key.F5)
                {
                    if (MDOgeneral.logBusquedadAbierta2 == true) { MDOgeneral.logBusquedadAbierta2 = false; }
                }
            }
        } 

    }

   
    public class anaConStn:ObservableObject 
    {
       #region singleton

        private static anaConStn instance = null;
        protected anaConStn()
        {
            consGrupo();
            MDOgeneral.conectar();
            strTituloVentana = "Comparativo material usado vs standard" + " - Usuario: " + MDOgeneral.usuario + " - [FRManaConStn]";
        }

        private string _strTituloVentana;
        public string strTituloVentana
        {
            get { return _strTituloVentana; }
            set { _strTituloVentana = value; base.OnPropertyChanged("strTituloVentana"); }
        }

        

        public static anaConStn Instance
        {
            get
            {
                if (instance == null)
                    instance = new anaConStn();
                return instance;
            }
            set 
            { 
                if (instance != null)
                    instance = null;
            }
        }
        #endregion

        private ObservableCollection<grupo> _grupos = new ObservableCollection<grupo>();
        public ObservableCollection<grupo> grupos
        {
            get { return _grupos; }
            set { _grupos = value; RaisePropertyChangedEvent("grupos"); }
        }

        public class grupo : ObservableObject
        {
            private Boolean _estaSel = true;
            public Boolean estaSel
            {
                get { return _estaSel; }
                set { _estaSel = value; RaisePropertyChangedEvent("estaSel"); }
            }

            private string _codgru;
            public string codgru
            {
                get { return _codgru; }
                set { _codgru = value; RaisePropertyChangedEvent("codgru"); }
            }

            private string _nomgru;
            public string nomgru
            {
                get { return _nomgru; }
                set { _nomgru = value; RaisePropertyChangedEvent("nomgru"); }
            }

        }
        public void consGrupo()
        {
            try
            {
                clases.CLSMAEGRP maegru = new clases.CLSMAEGRP();
                List<clases.CLSMAEGRP> grs = maegru.listaGrupos();
                foreach (clases.CLSMAEGRP r in grs)
                {
                    grupo a = new grupo { codgru = r.CODGRP_GRP, nomgru = r.NOMGRP_GRP };
                    grupos.Add(a);
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        string selgrupos;
        public void ConSelgrupos()
        {
            try
            {
                selgrupos = "";
                foreach (grupo r in grupos)
                {
                    if (r.estaSel)
                    {
                        if (String.IsNullOrEmpty(selgrupos))
                        {
                            selgrupos = r.codgru;
                        }
                        else
                        {
                            selgrupos = selgrupos + "-" + r.codgru;
                        }
                    }
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void selTodoslosGrupos()
        {
            foreach (grupo grp in grupos)
            {
                grp.estaSel = true;
            }
        }

        private void selNingunGrupo()
        {
            foreach (grupo grp in grupos)
            {
                grp.estaSel = false;
            }
        }

        private DateTime _desde = DateTime.Today;
        public DateTime desde 
        {
            get { return _desde; }
            set { _desde = value; RaisePropertyChangedEvent("desde"); }
        }

        private DateTime _hasta = DateTime.Today;
        public DateTime hasta 
        {
            get { return  _hasta; }
            set { _hasta = value; RaisePropertyChangedEvent("hasta"); }
        }

        private DataTable _informe = new DataTable();
        public DataTable informe 
        {
            get { return _informe; }
            set { _informe = value; RaisePropertyChangedEvent("informe"); }
        }

        private DataTable _informe2 = new DataTable();
        public DataTable informe2 
        {
            get { return _informe2; }
            set { _informe2 = value; RaisePropertyChangedEvent("informe2"); }
        }

        private estInf _selInf;
        public estInf selInf 
        {
            get { return _selInf; }
            set 
            {
                _selInf = value;
                if (_selInf != null) 
                {
                    visorFotoReferencia.strCodigo = _selInf.CODREF_TMP;
                    visorFotoReferencia.strColor = _selInf.CODCOL_TMP;
                }
                RaisePropertyChangedEvent("selInf");
            }        
        }

        private VMvisorFotoReferencia _visorFotoReferencia = new VMvisorFotoReferencia();
        public VMvisorFotoReferencia visorFotoReferencia
        {
            get { return _visorFotoReferencia; }
            set { _visorFotoReferencia = value; base.OnPropertyChanged("visorFotoReferencia"); }
        }

        private ObservableCollection<estInf> _lisEstinf = new ObservableCollection<estInf>();
        public ObservableCollection<estInf> lisEstinf 
        {
            get { return _lisEstinf; }
            set { _lisEstinf = value; RaisePropertyChangedEvent("lisEstinf"); }
        }
                        
        private DataTable detalle=new DataTable();

        public void procesar() 
        {
            try 
            {
                Mouse.OverrideCursor = Cursors.Wait;

                clases.CLSTEMPORAL tmp = new clases.CLSTEMPORAL();
                ConSelgrupos();
                tmp.SP_INFANACONS(selgrupos, desde, hasta);
                informe = tmp.TMPANACONS;
                lisEstinf.Clear();
                foreach (DataRow r in informe.Rows) 
                {
                    lisEstinf.Add(new estInf 
                    {
                        CODCOL_TMP = r["CODCOL_TMP"].ToString(),
                        CODGRP_TMP = r["CODGRP_TMP"].ToString(),
                        CODREF_TMP = r["CODREF_TMP"].ToString(),
                        CONPAR_TMP = Convert.ToDouble(r["CONPAR_TMP"]),
                        DEVUEL_TMP = Convert.ToDouble(r["DEVUEL_TMP"]),
                        ENTRE_TMP = Convert.ToDouble(r["ENTRE_TMP"]),
                        NOMCOL_TMP = r["NOMCOL_TMP"].ToString(),
                        NOMGRP_TMP = r["NOMGRP_TMP"].ToString(),
                        NOMREF_TMP = r["NOMREF_TMP"].ToString(),
                        PARCOR_TMP = Convert.ToInt32(r["PARCOR_TMP"]),
                        STAND_TMP = Convert.ToDouble(r["STAND_TMP"]),
                        VARIA_TMP = Convert.ToDouble(r["VARIA_TMP"])
                    });
                }

                Mouse.OverrideCursor = Cursors.Arrow;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }
        
        public void consDetalle() 
        {
            try 
            {
                clases.CLSTEMPORAL tmp = new clases.CLSTEMPORAL();
                tmp.SPD_INFANACONS(selInf.CODGRP_TMP, selInf.CODREF_TMP, selInf.CODCOL_TMP);
                informe2=tmp.TMPANACONS1;
                FRMmatCons frm = new FRMmatCons();
                frm.ShowDialog();             
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        public void imprimir() 
        {
            try 
            {
                DataSet ds = new DataSet("dsAnaConStn");
                DataTable encabezado = new DataTable("ENCABEZADO");
                encabezado.Columns.Add("DESDE");
                encabezado.Columns.Add("HASTA");
                DataRow r = encabezado.NewRow();
                r["DESDE"]=desde.ToShortDateString();
                r["HASTA"]=hasta.ToShortDateString();
                encabezado.Rows.Add(r);
                ds.Tables.Add(encabezado);
                informe.TableName="TMPANACONS";
                ds.Tables.Add(informe.Copy());
                Vista vista = new Vista { ds = ds, reporte = @"reportes\repAnaConStn.rpt" };
                vista.Show();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }
        
        public class estInf:ObservableObject 
        {
            string _CODGRP_TMP;
            public string CODGRP_TMP 
            {
                get { return _CODGRP_TMP; }
                set { _CODGRP_TMP = value; RaisePropertyChangedEvent("CODGRP_TMP"); }
            }

            string _NOMGRP_TMP;
            public string NOMGRP_TMP 
            {
                get { return _NOMGRP_TMP; }
                set { _NOMGRP_TMP = value; RaisePropertyChangedEvent("NOMGRP_TMP"); }
            }
                                        
            string _CODREF_TMP;
            public string CODREF_TMP 
            {
                get { return _CODREF_TMP; }
                set { _CODREF_TMP = value; RaisePropertyChangedEvent("CODREF_TMP"); }
            }

            string _CODCOL_TMP;
            public string CODCOL_TMP 
            {
                get { return _CODCOL_TMP; }
                set { _CODCOL_TMP = value; RaisePropertyChangedEvent("CODCOL_TMP"); }
            }

            string _NOMREF_TMP;
            public string NOMREF_TMP 
            {
                get { return _NOMREF_TMP; }
                set { _NOMREF_TMP = value; RaisePropertyChangedEvent("NOMREF_TMP"); }
            }

            string _NOMCOL_TMP;
            public string NOMCOL_TMP 
            {
                get { return _NOMCOL_TMP; }
                set { _NOMCOL_TMP=value; RaisePropertyChangedEvent("NOMCOL_TMP"); }
            }

            double _ENTRE_TMP;
            public double ENTRE_TMP 
            {
                get { return _ENTRE_TMP; }
                set { _ENTRE_TMP = value; RaisePropertyChangedEvent("ENTRE_TMP"); }
            }

            double _DEVUEL_TMP;
            public double DEVUEL_TMP 
            {
                get {return _DEVUEL_TMP; }
                set { _DEVUEL_TMP = value; RaisePropertyChangedEvent("DEVUEL_TMP"); }
            }

            double _STAND_TMP;
            public double STAND_TMP 
            {
                get { return _STAND_TMP; }
                set { _STAND_TMP = value; RaisePropertyChangedEvent("STAND_TMP"); }
            }

            double _VARIA_TMP;
            public double VARIA_TMP 
            {
                get { return _VARIA_TMP; }
                set { _VARIA_TMP = value; RaisePropertyChangedEvent("VARIA_TMP"); }
            }

            int _PARCOR_TMP;
            public  int PARCOR_TMP
            {
                get { return _PARCOR_TMP; }
                set { _PARCOR_TMP = value; RaisePropertyChangedEvent("PARCOR_TMP"); }
            } 

            double _CONPAR_TMP;
            public double CONPAR_TMP 
            {
                get { return _CONPAR_TMP; }
                set { _CONPAR_TMP = value; RaisePropertyChangedEvent("CONPAR_TMP"); }
            }
        }

        private BitmapImage _imagen1=new BitmapImage();
        public BitmapImage imagen1 
        {
            get { return _imagen1; }
            set { _imagen1 = value; RaisePropertyChangedEvent("imagen1"); }
        }
          

    }
}
